import { baseApiUrl, path } from "../../utils/config";
import { productInfo, orderPlace } from '../../utils/api';
const app = getApp();
Page({
  data: {
    baseImageUrl: baseApiUrl,
    id: null,
    index: 1,
    list: [],

    thumbnail: "",
    price: 0,
    name: "",
    synopsis: "",
    freight: "",
    service: [],
    brand: "",
    number: "",
    content: "",

    show: false,
    size_list: [
      { id: 1, name: "100cm", inventory: 10 },
      { id: 2, name: "200cm", inventory: 10 },
      { id: 3, name: "300cm", inventory: 0 },
      { id: 4, name: "400cm", inventory: 0 },
      { id: 5, name: "500cm", inventory: 10 },
    ],
    size_id: "",
    color_list: [
      { id: 1, name: "红色", inventory: 10 },
      { id: 2, name: "蓝色", inventory: 10 },
    ],
    color_id: "",
    count: 1,
    total: 0.00
  },
  onLoad(options) {
    let { id } = options;
    this.setData({ id });
    this.getDeta();
  },
  async getDeta() {
    let id = this.data.id;
    let res = await productInfo({ id });
    res = res.product;
    let { imgs, thumbnail, price, name, synopsis, freight, service, brand, number, content } = res;

    let { count } = this.data;
    let total = (Number(price) * Number(count)).toFixed(4);

    this.setData({ list: imgs, thumbnail, price, name, synopsis, freight, service, brand, number, content, total });
  },
  sizeChange(e) {
    const item = e.currentTarget.dataset.item;
    if (item.inventory <= 0) return false;
    let id = item.id;
    this.setData({ size_id: id });
  },
  colorChange(e) {
    const item = e.currentTarget.dataset.item;
    let id = item.id;
    this.setData({ color_id: id });
  },
  change(e) {
    let index = Number(e.detail.current) + 1;
    this.setData({ index })
  },
  open() {
    this.setData({ show: true })
  },
  onClose() {
    this.setData({ show: false })
  },
  baseChange(e) {
    let count = e.detail;
    let price = this.data.price;
    let total = (Number(price) * Number(count)).toFixed(4);
    this.setData({ count, total });
  },
  async router(e) {
    let { id, count } = this.data;
    let res = await orderPlace({
      product_id: id,
      number: count
    });
    let url = `/pages/order_deta/order_deta?id=${res?.id}`;
    wx.navigateTo({ url });
  },
  onHide() { this.onClose() },
  onShareAppMessage() {
    let poster = app.globalData.poster;
    return {
      path,
      imageUrl: poster
    }
  },
  onShareTimeline() { }
})